<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Banneritem;
use App\Http\Requests\CreateBanneritemRequest;
use Illuminate\Support\Facades\Storage;
use MyFlash;
use App\Banner;
use Image;
class BanneritemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list=Banneritem::OrderBy("sort","Desc")->OrderBy("id","Desc")->get();
        return view('admin.banneritem.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $banners=Banner::all();
        return view('admin.banneritem.create',compact('banners'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateBanneritemRequest $request)
    {
        $ban=new Banneritem;
        $ban->title=$request->title;
        $ban->banner_id=$request->banner_id;
        $ban->digest=$request->digest;
        $ban->sort=$request->sort;
        $ban->isshow=isset($request->isshow)?$request->isshow:'0';
        $ban->pic=$this->thumbil($request);
        if($ban->save()){
            MyFlash::success('轮播图添加成功');
            return redirect()->route('admin.banneritem.index');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $banners=Banner::all();
        $list=Banneritem::findOrFail($id);
        return view('admin.banneritem.edit',compact('banners','list'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CreateBanneritemRequest $request, $id)
    {
        $list=Banneritem::findOrFail($id);
        $list->title=$request->title;
        $list->sort=$request->sort;
        $list->digest=$request->digest;
        if(isset($request->isshow)){
            $list->isshow=$request->isshow;
        }
        if($request->file('pic')){
            $list->pic=$this->thumbil($request);
            $list->exitsimg($list->pic);
        }
        if($list->save()){
            MyFlash::success('轮播图编辑成功');
            return redirect()->route('admin.banneritem.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $case=Banneritem::findOrFail($id);
        $blur=Banneritem::where('id',$case->id)->delete();
        $this->exitsimg($case->pic);
        if($blur){
            MyFlash::success('删除成功');
            return redirect()->route('admin.banneritem.index');
        }
    }
    /**
     * 图片上传处理
     */
    public function thumbil($request){
        if ($request->file('pic')) {
            $thumb=$request->pic;
            $name=$thumb->hashName();
            // $thumb->storeAs('/public/thumb/',$name);
            $path=storage_path('app/public/uploads/'.$name);
            Image::make($thumb)->resize(370,260)->save($path);
            return $name;
        }

    }
    // 检查文件是否存在
    public function exitsimg($img){
        $path='public/uploads/'.$img;
        $exists = Storage::disk('local')->exists($path);
        if ($exists) {
            $this->delimg($path);
        }
    }
    // 删除图片
    public function delimg($path){
        $result=Storage::delete($path);
        if ($result) {
            return true;
        }else {
            return false;
        }
    }
    //排序
    public function setsort(Request $request){
        if($request->isMethod('post')){
            $ban=Banneritem::find($request->id);
            $ban->sort=$request->sort;
            $res=$ban->save();

            if($res>0){
                return ['code'=>1,'操作成功'];
            }
            return ['code'=>0,'操作失败'];
        }
    }
}
